影像翻轉
1.1 水平翻轉(以Y軸翻轉)
import cv2
# 顯示圖檔
def show_img(name, img):
cv2.imshow(name, img)
cv2.waitKey(0)
image_path = './over_brain.jpg'
image = cv2.imread(image_path)
show_img('origin', image)
# 水平翻轉
image1 = cv2.flip(image, 1)
show_img('level', image1)
1.2 垂直翻轉(以X軸翻轉)
import cv2
# 顯示圖檔
def show_img(name, img):
cv2.imshow(name, img)
cv2.waitKey(0)
image_path = './over_brain.jpg'
image = cv2.imread(image_path)
show_img('origin', image)
# 水平翻轉
image1 = cv2.flip(image, 0)
show_img('vertical', image1)
影像旋轉
2.1 90度旋轉
程式碼
import cv2
import numpy as np
# 顯示圖檔
def show_img(name, img):
cv2.imshow(name, img)
cv2.waitKey(0)
image_path = './over_brain.jpg'
image = cv2.imread(image_path)
image = cv2.resize(image, (300, 300))
# 90度旋轉
image1 = np.rot90(image, 0)
show_img('no rotation', image1)
image2 = np.rot90(image, 1)
show_img('counterclockwise90', image2)
image3 = np.rot90(image, -1)
show_img('clockwise90', image3)
參數:n=1代表逆時針旋轉90度、n=-1代表順時針旋轉90度
執行結果
2.1 任意角度旋轉
程式碼
import cv2
# 顯示圖檔
def show_img(name, img):
cv2.imshow(name, img)
cv2.waitKey(0)
image_path = './over_brain.jpg'
image = cv2.imread(image_path)
image = cv2.resize(image, (400, 400))
show_img('origin', image)
(h, w, c) = image.shape
center = (w // 2, h // 2)
# 代表逆時針旋轉30度,縮放倍數為1倍
M = cv2.getRotationMatrix2D(center, 30, 1)
# (w, h )代表圖片縮放與旋轉後,需裁切成的尺寸
image1 = cv2.warpAffine(image, M, (w, h))
show_img('counterclockwise30', image1)
執行結果
影像放大縮小
3.1 指定像素
程式碼
import cv2
# 顯示圖檔
def show_img(name, img):
cv2.imshow(name, img)
cv2.waitKey(0)
image_path = './over_brain.jpg'
image = cv2.imread(image_path)
show_img('origin', image)
image1 = cv2.resize(image, (250, 250))
show_img('resize', image1)
interpolation:在圖片縮放時,會影響圖片縮放計算速度與細節保留程度。詳細可參考opencv中插值算法详解。
執行結果
3.2 比例縮放
程式碼
import cv2
# 顯示圖檔
def show_img(name, img):
cv2.imshow(name, img)
cv2.waitKey(0)
image_path = './over_brain.jpg'
image = cv2.imread(image_path)
show_img('origin', image)
image1 = cv2.resize(image, dsize=None, fx=0.5, fy=0.3)
show_img('resize', image1)
參數:fx代表水平(X軸)縮放倍率、fy代表垂直(Y軸)縮放倍率。
執行結果
影像分割與合併
4.1 影像分割
程式碼
import cv2
# 顯示圖檔
def show_img(name, img):
cv2.imshow(name, img)
cv2.waitKey(0)
image_path = './over_brain.jpg'
image = cv2.imread(image_path)
show_img('origin', image)
# 起點座標
start_point = (50, 100)
# 剪裁區域的寬與高
(w, h) = (250, 300)
image1 = image[start_point[0]:start_point[0]+w, start_point[1]:start_point[1]+h]
show_img('crop', image1)
執行結果
4.2 影像合併
圖像大小一致
程式碼
import cv2
# 顯示圖檔
def show_img(name, img):
cv2.imshow(name, img)
cv2.waitKey(0)
image1_path = './over_brain.jpg'
image2_path = './peach.jpg'
image1 = cv2.imread(image1_path)
image1 = cv2.resize(image1, (250, 250))
image2 = cv2.imread(image2_path)
image2 = cv2.resize(image2, (250, 250))
show_img('image1', image1)
show_img('image2', image2)
# 垂直合併
vertical_image = cv2.vconcat([image1, image2])
show_img('vertical_image', vertical_image)
# 水平合併
level_image = cv2.hconcat([image1, image2])
show_img('level_image', level_image)
執行結果
圖像大小不一致
程式碼
import cv2
import numpy as np
# 顯示圖檔
def show_img(name, img):
cv2.imshow(name, img)
cv2.waitKey(0)
image1_path = './over_brain.jpg'
image2_path = './peach.jpg'
image1 = cv2.imread(image1_path)
image1 = cv2.resize(image1, (220, 220))
image2 = cv2.imread(image2_path)
image2 = cv2.resize(image2, (130, 130))
show_img('image1', image1)
show_img('image2', image2)
h1, w1 = image1.shape[:2]
h2, w2 = image2.shape[:2]
# 水平合併
image3 = np.zeros((max(h1, h2), w1+w2,3), dtype=np.uint8)
image3[:h1, :w1,:3] = image1
image3[:h2, w1:w1+w2,:3] = image2
show_img('level', image3)
# 垂直合併
image4 = np.zeros((h1+h2, max(w1, w2),3), dtype=np.uint8)
image4[:h1, :w1,:3] = image1
image4[h1:h1+h2, :w2,:3] = image2
show_img('vertical', image4)
執行結果
讓我們繼續看下去...